package com.classDemo;
import java.util.ArrayList;
import java.util.Scanner;

public class studentMannerage {
    public static void main(String[] args) {
        ArrayList<student> studentArrayList = new ArrayList<student>();
        while (true){
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 退出");
            System.out.println("请输入你的选择：");
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            switch (line){
                case "1":
                    addStudent(studentArrayList);
                    break;
                case "2":
                    deleteStudent(studentArrayList);
                    break;
                case "3":
                    updateStudent(studentArrayList);
                    break;
                case "4":
                    findAllStudent(studentArrayList);
                    break;
                case "5":
                    System.out.println("感谢使用");
                    System.exit(0);

            }
        }
    }
    public static  void addStudent(ArrayList<student> studentArrayList){
       while (true){
           Scanner sc = new Scanner(System.in);
           System.out.println("请输入学生学号");
           String sid = sc.nextLine();
           boolean i = IsUsed(studentArrayList,sid);
           if (i){
               System.out.println("学号重复请重新输入");
           }
           System.out.println("请输入学生姓名");
           String name = sc.nextLine();
           System.out.println("请输入学生性别");
           String sex = sc.nextLine();
           System.out.println("请输入学生年龄");
           int age = sc.nextInt();
           System.out.println("请输入学生住址");
           String address = sc.nextLine();
           student s = new student();
           s.setSid(sid);
           s.setName(name);
           s.setSex(sex);
           s.setAge(age);
           s.setAddress(address);
           studentArrayList.add(s);
           System.out.println("添加学生成功");
            break;

       }
    }
    public static boolean IsUsed(ArrayList<student>studentArrayList,String sid){
        boolean flag = false;
        for(int i=0;i<studentArrayList.size();i++){
            student s = studentArrayList.get(i);
            if(s.getSid().equals(sid)){
                flag = true;
                break;
            }
        }
        return flag;
    }
    public static void deleteStudent(ArrayList<student> studentArrayList){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除学生的学号：");
        String sid = sc.nextLine();
        int index = -1;
        for (int i=0;i<studentArrayList.size();i++){
            student s = studentArrayList.get(i);
            if (s.getSid().equals(sid)){
                index=i;
                break;
            }
        }
        if (index==-1){
            System.out.println("该信息不存在，请重新输入");
        }else {
            studentArrayList.remove(index);
            System.out.println("删除成功！");
        }
    }
    public static  void  updateStudent(ArrayList<student> studentArrayList){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的学号");
        String sid = sc.nextLine();
        System.out.println("请输入要修改学生的姓名");
        String name = sc.nextLine();
        System.out.println("请输入要修改学生的性别");
        String sex = sc.nextLine();
        System.out.println("请输入要修改学生的年龄");
        int age = sc.nextInt();
        System.out.println("请输入要修改学生的住址");
        String address = sc.nextLine();
        student s = new student();
        s.setSid(sid);
        s.setName(name);
        s.setSex(sex);
        s.setAge(age);
        s.setAddress(address);
        for (int i=0;i<studentArrayList.size();i++){
            student student = studentArrayList.get(i);
            if (student.getSid().equals(sid)){
                studentArrayList.set(i,s);
                break;
            }
        }
        System.out.println("修改成功！");
    }
    public static  void findAllStudent(ArrayList<student> studentArrayList){
        if(studentArrayList.size()==0){
            System.out.println("没有信息，请输入信息后查看");
            return;
        }
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t性别\t\t居住地");
        for (int i=0;i<studentArrayList.size();i++){
            student s = studentArrayList.get(i);
            System.out.println(s.getStudent());
        }
        System.out.println("输入任意键返回主菜单");
        Scanner sc = new Scanner(System.in);
        sc.nextLine();
        return;
    }
}
